package Other;

import java.util.*;

public class TreeTraversals 
{
	private static  List<Integer> vals = new ArrayList<Integer>();
	
	public static void main(String[] args) {
		//Create a test BST.
		TreeNode node = new TreeNode(50);
		node.setLeft(new TreeNode(12));
		node.setRight(new TreeNode(75));
		node.getLeft().setRight(new TreeNode(25));
		node.getLeft().setLeft(new TreeNode(1));
		node.getRight().setLeft(new TreeNode(51));
		node.getRight().setRight(new TreeNode(85));
		node.getRight().getRight().setLeft(new TreeNode(84));
		
		System.out.println(isBST(node));
		
		printBST(node);
		
	}
	
	static void printBST(TreeNode node)
	{
		if(node == null)
		{
			return;
		}
		else
		{
			printBST(node.getLeft());
			System.out.println(node.getValue());
			printBST(node.getRight());
		}
	}
	
	static boolean isBST(TreeNode node)
	{
		//Clear the array.
		vals = new ArrayList<Integer>();
		
		//Traverse the tree.
		inorderTraversal(node);
		
		//Iterate through the traversed values, checking if they are sorted. 
		int prev = Integer.MIN_VALUE;
		for(Iterator<Integer> iter = vals.iterator(); iter.hasNext();)
		{
			int current = iter.next();
			
			if(prev > current)
			{
				return false;
			}
			
			prev = current;
		}	
		
		return true;
	}
	
	private static void inorderTraversal(TreeNode node)
	{
		if(node != null)
		{
			inorderTraversal(node.getLeft());
			vals.add(node.getValue());
			inorderTraversal(node.getRight());
		}
	}
}
